package com.ruoyi.finish.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.finish.domain.FinishClassInfo;
import com.ruoyi.train.domain.*;
import com.ruoyi.train.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finish.service.IapprovalFinishService;

/**
 * 审批完成信息Service业务层处理
 * 
 * @author wusihao
 * @date 2023-05-10
 */
@Service
public class approvalFinishServiceImpl implements IapprovalFinishService 
{
    @Autowired
    private ICertApplicationService certApplicationService;
    @Autowired
    private ICertApplicationStudentService certApplicationStudentService;
    @Autowired
    private IExamApplicationService examApplicationService;
    @Autowired
    private IExamApplicationStudentService examApplicationStudentService;
    @Autowired
    private ITrainClassService trainClassService;
    @Autowired
    private IStudentInformationService studentInformationService;
    @Autowired
    private IStudentProcessRecordService studentProcessRecordService;
    @Autowired
    private ITrainPlanService trainPlanService;

    @Autowired
    private IExamPassCertificateService examPassCertificateService;

    @Autowired
    private IExamPassStudentService examPassStudentService;

    @Override
    public List<FinishClassInfo> selectapprovalFinishList(FinishClassInfo finishClassInfo)
    {
        // 存储答案列表
        List<FinishClassInfo> finishClassInfoList = new ArrayList<>();
        // 查询已经通过办证申请表的信息
        QueryWrapper<CertApplication> certApplicationQueryWrapper = new QueryWrapper<>();
        certApplicationQueryWrapper.eq("approval_process_sign", 4);
        certApplicationQueryWrapper.eq("is_deleted", 0);
        certApplicationQueryWrapper.eq("is_pass", 1);
        List<CertApplication> certApplicationList = certApplicationService.list(certApplicationQueryWrapper);
        // 通过办证申请表信息查询已经通过的考试申请表信息
        for (CertApplication certApplication : certApplicationList) {
            FinishClassInfo finishTemp = new FinishClassInfo();
            finishTemp.setId(certApplication.getId());
            // 通过办证申请表id得到办证申请表学生信息
            Long applicationId = certApplication.getId();
            QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
            certApplicationStudentQueryWrapper.eq("cert_application_id", applicationId);
            List<CertApplicationStudent> certApplicationStudentList = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
            // 通过学生id得到考试申请表学生表信息
            Long studentId = certApplicationStudentList.get(0).getStudentId();
            if (studentId == null) continue;
            QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
            examApplicationStudentQueryWrapper.eq("student_id", studentId);
            List<ExamApplicationStudent> examApplicationStudentList = examApplicationStudentService.list(examApplicationStudentQueryWrapper);
            Long examApplicationId = examApplicationStudentList.get(0).getExamApplicationId();
            // 通过考试申请表id得到考试申请表
            QueryWrapper<ExamApplication> examApplicationQueryWrapper = new QueryWrapper<>();
            examApplicationQueryWrapper.eq("approval_process_sign", 6);
            examApplicationQueryWrapper.eq("is_pass", 1);
            examApplicationQueryWrapper.eq("is_deleted", 0);
            examApplicationQueryWrapper.eq("id", examApplicationId);
            ExamApplication examApplicationServiceOne = examApplicationService.getOne(examApplicationQueryWrapper);
            if (examApplicationServiceOne == null) continue;

            // 得到成绩汇总表，考场情况记录表附件
            finishTemp.setGradeSet(examApplicationServiceOne.getGradeSet());
            finishTemp.setExamplaceRecord(examApplicationServiceOne.getExamplaceRecord());

            // 通过学生id得到班级信息
            QueryWrapper<StudentInformation> studentInformationQueryWrapper = new QueryWrapper<>();
            studentInformationQueryWrapper.eq("is_deleted", 0);
            studentInformationQueryWrapper.eq("id", studentId);
            StudentInformation studentInformationServiceOne = studentInformationService.getOne(studentInformationQueryWrapper);
            if (studentInformationServiceOne == null) continue;
            // 通过学生信息找到学生记录表信息
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq("is_deleted", 0);
            studentProcessRecordQueryWrapper.eq("serial_number", studentInformationServiceOne.getSerialNumber());
            studentProcessRecordQueryWrapper.eq("exam_application_is_pass" , 1);
            studentProcessRecordQueryWrapper.eq("exam_certificate_is_pass", 1);
            studentProcessRecordQueryWrapper.eq("cert_is_pass", 1);
            StudentProcessRecord studentProcessRecordServiceOne = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
            if (studentProcessRecordServiceOne == null) continue;
            // 通过学生记录表信息得到班级id
            Long classId = studentProcessRecordServiceOne.getClassId();
            QueryWrapper<TrainClass> trainClassQueryWrapper = new QueryWrapper<>();
            trainClassQueryWrapper.eq("is_deleted", 0);
            trainClassQueryWrapper.eq("id", classId);
            TrainClass trainClassServiceOne = trainClassService.getOne(trainClassQueryWrapper);
            // 将班级信息存入答案集合
            finishTemp.setTrainPeriod(trainClassServiceOne.getTrainPeriod());
            finishTemp.setUrlClassTable(trainClassServiceOne.getUrlClassTable());
            finishTemp.setUrlApplication(trainClassServiceOne.getUrlApplication());
            finishTemp.setUrlSign(trainClassServiceOne.getUrlSign());
            finishTemp.setUrlInvalue(trainClassServiceOne.getUrlInvalue());
            finishTemp.setUrlCertificate(trainClassServiceOne.getUrlCertificate());

            // 通过学生记录表得到培训计划信息
            Long trainPlanId = studentProcessRecordServiceOne.getTrainPlanId();
            QueryWrapper<TrainPlan> trainPlanQueryWrapper = new QueryWrapper<>();
            trainPlanQueryWrapper.eq("id", trainPlanId);
            TrainPlan trainPlanServiceOne = trainPlanService.getOne(trainPlanQueryWrapper);
            finishTemp.setTrainType(trainPlanServiceOne.getTrainType());
            finishTemp.setTrainMethod(trainPlanServiceOne.getTrainMethod());

            finishClassInfoList.add(finishTemp);
        }
        return finishClassInfoList;
    }

    @Override
    public FinishClassInfo checkFinishInformation(Long applicationId) {
        FinishClassInfo finishTemp = new FinishClassInfo();
        finishTemp.setId(applicationId);
        // 通过办证申请表id得到办证申请表pdf
        CertApplication certApplicationServiceById = certApplicationService.getById(applicationId);
        finishTemp.setCertApplicationPdf(certApplicationServiceById.getCertApplicationPdf());
        // 通过办证申请表id得到办证申请表学生信息
        QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
        certApplicationStudentQueryWrapper.eq("cert_application_id", applicationId);
        List<CertApplicationStudent> certApplicationStudentList = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
        Long studentId = certApplicationStudentList.get(0).getStudentId();
        // 通过学生id得到考试合格证明表信息
        QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
        examPassStudentQueryWrapper.eq("student_id", studentId);
        List<ExamPassStudent> examPassCertificates = examPassStudentService.list(examPassStudentQueryWrapper);
        Long examPassId = examPassCertificates.get(0).getExamPassId();
        QueryWrapper<ExamPassCertificate> examPassCertificateQueryWrapper = new QueryWrapper<>();
        examPassCertificateQueryWrapper.eq("is_pass", 1);
        examPassCertificateQueryWrapper.eq("is_deleted", 0);
        examPassCertificateQueryWrapper.eq("approval_process_sign", 4);
        examPassCertificateQueryWrapper.eq("id", examPassId);
        ExamPassCertificate examPassCertificateServiceOne = examPassCertificateService.getOne(examPassCertificateQueryWrapper);
        finishTemp.setExamCertificatePdf(examPassCertificateServiceOne.getExamCertificatePdf());

        // 通过学生id得到考试申请表学生表信息
        QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
        examApplicationStudentQueryWrapper.eq("student_id", studentId);
        List<ExamApplicationStudent> examApplicationStudentList = examApplicationStudentService.list(examApplicationStudentQueryWrapper);
        Long examApplicationId = examApplicationStudentList.get(0).getExamApplicationId();
        // 通过考试申请表id得到考试申请表
        QueryWrapper<ExamApplication> examApplicationQueryWrapper = new QueryWrapper<>();
        examApplicationQueryWrapper.eq("approval_process_sign", 6);
        examApplicationQueryWrapper.eq("is_pass", 1);
        examApplicationQueryWrapper.eq("is_deleted", 0);
        examApplicationQueryWrapper.eq("id", examApplicationId);
        ExamApplication examApplicationServiceOne = examApplicationService.getOne(examApplicationQueryWrapper);
        // 得到成绩汇总表，考场情况记录表附件
        finishTemp.setGradeSet(examApplicationServiceOne.getGradeSet());
        finishTemp.setExamplaceRecord(examApplicationServiceOne.getExamplaceRecord());
        finishTemp.setExamApplicationPdf(examApplicationServiceOne.getExamApplicationPdf());

        // 通过学生id得到班级信息
        QueryWrapper<StudentInformation> studentInformationQueryWrapper = new QueryWrapper<>();
        studentInformationQueryWrapper.eq("is_deleted", 0);
        studentInformationQueryWrapper.eq("id", studentId);
        StudentInformation studentInformationServiceOne = studentInformationService.getOne(studentInformationQueryWrapper);
        // 通过学生信息找到学生记录表信息
        QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
        studentProcessRecordQueryWrapper.eq("is_deleted", 0);
        studentProcessRecordQueryWrapper.eq("serial_number", studentInformationServiceOne.getSerialNumber());
        studentProcessRecordQueryWrapper.eq("exam_application_is_pass" , 1);
        studentProcessRecordQueryWrapper.eq("exam_certificate_is_pass", 1);
        studentProcessRecordQueryWrapper.eq("cert_is_pass", 1);
        StudentProcessRecord studentProcessRecordServiceOne = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
        // 通过学生记录表信息得到班级id
        Long classId = studentProcessRecordServiceOne.getClassId();
        QueryWrapper<TrainClass> trainClassQueryWrapper = new QueryWrapper<>();
        trainClassQueryWrapper.eq("is_deleted", 0);
        trainClassQueryWrapper.eq("id", classId);
        TrainClass trainClassServiceOne = trainClassService.getOne(trainClassQueryWrapper);
        // 将班级信息存入答案集合
        finishTemp.setTrainPeriod(trainClassServiceOne.getTrainPeriod());
        finishTemp.setUrlClassTable(trainClassServiceOne.getUrlClassTable());
        finishTemp.setUrlApplication(trainClassServiceOne.getUrlApplication());
        finishTemp.setUrlSign(trainClassServiceOne.getUrlSign());
        finishTemp.setUrlInvalue(trainClassServiceOne.getUrlInvalue());
        finishTemp.setUrlCertificate(trainClassServiceOne.getUrlCertificate());

        // 通过学生记录表得到培训计划信息
        Long trainPlanId = studentProcessRecordServiceOne.getTrainPlanId();
        QueryWrapper<TrainPlan> trainPlanQueryWrapper = new QueryWrapper<>();
        trainPlanQueryWrapper.eq("id", trainPlanId);
        TrainPlan trainPlanServiceOne = trainPlanService.getOne(trainPlanQueryWrapper);
        finishTemp.setTrainType(trainPlanServiceOne.getTrainType());
        finishTemp.setTrainMethod(trainPlanServiceOne.getTrainMethod());
        finishTemp.setTrainplanApplication(trainPlanServiceOne.getTrainplanApplication());

        return finishTemp;
    }

}
